import axios from 'axios'
import { showFailToast, closeToast } from 'vant';
import store from '@/store';

const axiosInstance = axios.create({
    baseURL: "http://127.0.0.1:3000/v1",
    timeout: 5000
})

// 请求拦截器
axiosInstance.interceptors.request.use(config => {
    if (store.state.token) {
        config.headers['softeem_token'] = store.state.token
    }
    return config
})

// 响应拦截器
axiosInstance.interceptors.response.use(resp => {
    if (resp.data.status == 'success') {
        return Promise.resolve(resp.data.data)
    } else {
        console.log("逻辑错误");
        closeToast()
        showFailToast(resp.data.msg);
        return Promise.resolve(resp.data)
    }
}, error => {
    closeToast()
    showFailToast('请求失败，请重试');
    return Promise.reject(error)
})

export default axiosInstance